/*
 * @Author: BaiYu
 * @Date: 2021-03-18 19:49:44
 * @LastEditTime: 2021-06-03 15:07:43
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \STM32_Proj\Source\Dev\protocol.c
 */
#include "config.h"
#include "protocol.h"

/**
 * @description: checksum(CRC16)
 * @param {const void} *pBuffer
 * @param {uint16_t} BufferSize
 * @return {uint16_t}
 */
uint16_t ProtocolCRC(const void *pBuffer, uint16_t bufferSize)
{
	const uint8_t *pBytesArray = (const uint8_t *)pBuffer;
	uint16_t poly = 0x8408;
	uint16_t crc = 0;
	uint8_t carry;
	for (uint16_t j = 0; j < bufferSize; j++)
	{
		{
			crc = crc ^ pBytesArray[j];
			for (uint8_t i_bits = 0; i_bits < 8; i_bits++)
			{
				carry = crc & 1;
				crc = crc / 2;
				if(carry)
				{
					crc = crc ^ poly;
				}
			}
		}
	}
	return crc;
}


